
package com.suarte.core.hibernate;
import com.suarte.core.Company;
import com.suarte.core.Contact;
import com.suarte.core.Remission;
import com.suarte.core.RemissionDetail;
import com.suarte.core.dao.RemissionDao;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.appfuse.dao.hibernate.GenericDaoHibernate;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @date   Dec 20, 2010
 * @author Ggutierrez
 */
@Repository("remissionDao")
public class RemissionDaoHibernate extends GenericDaoHibernate<Remission, Long> implements RemissionDao {
    
    public RemissionDaoHibernate() {
        super(Remission.class);
    }

    public List<Remission> findByDescription(String description) {
        return getHibernateTemplate().find("from Remission where description like upper(?)", description.concat("%").toUpperCase());
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void store(Remission object) {
        getHibernateTemplate().saveOrUpdate(object);
    }

    @Override
    public Remission get(Long id) {
        List<Remission> results = getHibernateTemplate().find("from Remission this join fetch this.details where this.id = ?", id);
        return !results.isEmpty() ? results.get(0) : null;
    }

    public void deleteDetails(List<RemissionDetail> args) {
        getHibernateTemplate().deleteAll(args);
    }

    public List<Remission> findRemissions(Long id, Long quotationId, Long workOrderId, String description, Date date, Company company, Contact contact){
        String hql = "from Remission this " +
                     " join fetch this.workOrder workOrder " +
                     " join fetch workOrder.quotation quotation " +
                     " join fetch quotation.contact contact " +
                     " left join fetch this.employee employee " +
                     " left join fetch employee.user user " +
                     " where 1 = 1";
        List args = new ArrayList();

        if (id != null){
            args.add(id);
            hql += " and this.id = ?)";
        }

        if (quotationId != null){
            args.add(quotationId);
            hql += " and quotation.id = ?)";
        }

        if (workOrderId != null){
            args.add(workOrderId);
            hql += " and workOrder.id = ?)";
        }

        if (!isBlank(description)) {
            args.add(description.toUpperCase().concat("%"));
            hql += " and upper(ltrim(rtrim(this.description))) like upper(ltrim(rtrim(?)))";
        }
        if (date != null) {
            args.add(date);
            hql += " and (this.date) = (?)";
        }
        if (company != null) {
            args.add(company);
            hql += " and contact.company = ?";
        }
        if (contact != null) {
            args.add(contact);
            hql += " and contact = ?";
        }

        List results = getHibernateTemplate().find(hql, args.toArray());
        return results;
    }

    private boolean isBlank(String value) {
        return value == null || value.trim().length() == 0;
    }
}
